﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Itasa.API
{
    internal class Episode : IComparable, IComparable<Episode>
    {

        internal long ShowID { get; private set; }
        internal String ShowName { get; private set; }
        internal String Number { get; private set; }
        internal String Title { get; private set; }
        internal DateTime Date { get; private set; }

        internal Episode(String ShowName, long ShowID, String[] data)
        {
            this.ShowName = ShowName;
            this.Number = data[0];
            this.Title = data[1];
            DateTime date;
            if (!DateTime.TryParse(data[2], out date)) date = DateTime.MaxValue;
            Date = date;
        }

        internal Episode(XmlNode episode_node)
        {
            foreach (XmlNode ed in episode_node.ChildNodes)
            {
                if (ed.InnerText != null)
                {
                    if (ed.Name == "show_id") ShowID = long.Parse(ed.InnerText);
                    if (ed.Name == "show_name") ShowName = ed.InnerText;
                    if (ed.Name == "number") Number = ed.InnerText;
                    if (ed.Name == "title") Title = ed.InnerText;
                    if (ed.Name == "date")
                    {
                        DateTime data;
                        if (!DateTime.TryParse(ed.InnerText, out data)) data = DateTime.MaxValue;
                        Date = data;
                    }
                }
            }
        }

        public override string ToString()
        {
            return Number + " - " + Title + " - " + Date.ToShortDateString();
        }

        public string ToShortString()
        {
            return Number + " - " + Title;
        }

        public int CompareTo(object obj)
        {
            if (obj is Episode)
            {
                Episode e = (obj as Episode);
                return this.Date.CompareTo((obj as Episode).Date);
            }
            else return 0;
        }

        public int CompareTo(Episode other)
        {
            if (other == null) throw new ArgumentNullException("other");
            return this.Date.CompareTo(other.Date);
        }
    }
}
